/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.coordinador;

import DBMS.DataBaseManager;
import clases.Aspirante;
import clases.ComprobanteAspirantes;
import clases.Configuracion;
import clases.Coordinador;
import clases.Institucion;
import clases.InstitucionAspirante;
import clases.TipoReporte;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author tamerdark
 */
public class GenerarComprobante extends DispatchAction {

    public ActionForward page(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager dbms = DataBaseManager.getInstance();
        Coordinador c = new Coordinador();
        c.setCedula((String) request.getSession().getAttribute("cedula"));
        ArrayList cs = dbms.consultarCoordinador(c);
        Iterator i = cs.iterator();
        Coordinador a = (Coordinador) i.next();
        request.setAttribute("instituciones", a.getInstituciones());
        return mapping.findForward("page");
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession s = request.getSession();
        request.setAttribute("instituciones", Institucion.getAll());
        Aspirante a = new Aspirante();
        a.setInstitucionId((Integer) s.getAttribute("institucionId_coordinador"));
        DataBaseManager dbms = DataBaseManager.getInstance();
        request.setAttribute("aspirantes", dbms.consultarAspirantes(a));
        return mapping.findForward("failure");
    }

    public ActionForward make(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager d = DataBaseManager.getInstance();
        HttpSession s = request.getSession();
        ComprobanteAspirantes reporte = (ComprobanteAspirantes) form;
        reporte.setCedulaCoordinador(Integer.valueOf(String.valueOf(s.getAttribute("cedula"))));
        reporte.setTipo(TipoReporte.ASPIRANTE);
        reporte.setNumAutenticacion();
        Coordinador c = new Coordinador();
        c.setCedula(String.valueOf(reporte.getCedulaCoordinador()));
        List list = d.consultarCoordinador(c);
        c = (Coordinador) list.get(0);
        reporte.setCoordinador(c);
        Institucion i = new Institucion();
        i.setId(reporte.getInstitucionId());
        reporte.setInstitucion((Institucion) (d.consultarInstitucion(i)).get(0));
        while (d.existeNumAutenticacion(reporte.getNumAutenticacion())) {
            reporte.setNumAutenticacion();
        }
        d.agregarReporte(reporte);
        reporte.setId(d.buscarID(reporte.getNumAutenticacion()));
        Aspirante a = new Aspirante();
        a.setInstitucionId(reporte.getInstitucionId());
        for (InstitucionAspirante ia : d.consultarAspirantes(a)) {
            if (ia.getId() == reporte.getInstitucionId()) {
                reporte.setAspirantes(ia.getAspirantes());
            }
        }
        String path = Configuracion.getReportesPath() + reporte.getTipo() + reporte.getId() + ".pdf";
        s.setAttribute("reporte", path);
        request.setAttribute("reporte", path);
        try {
            reporte.generar(path);
            return mapping.findForward("success");
        } catch (Exception e) {
            reporte.setMensaje("Problemas con el servidor.");
            return mapping.findForward("failure");
        }
    }
}
